package com.endbd.warehouse.controller;

import com.endbd.feignapi.api.MarketingApi;
import com.endbd.feignapi.model.marketing.Orderdetails;
import com.endbd.feignapi.model.warehouse.CkWarehouse;
import com.endbd.feignapi.util.PageModel;
import com.endbd.feignapi.util.ResultMessage;
import com.endbd.warehouse.model.CKCondition;
import com.endbd.warehouse.model.IDCondition;
import com.endbd.warehouse.service.IManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/warehouse")
public class CKWarehouseAction {
    @Autowired
    private IManageService manageService;
    public IManageService getManageService() {
        return manageService;
    }
    public void setManageService(IManageService manageService) {
        this.manageService = manageService;
    }

    @Autowired
    private MarketingApi marketingApi;

    @RequestMapping("/ckPageBean")
    @ResponseBody
    public PageModel<CkWarehouse> queryRK(CKCondition ckc, PageModel<CkWarehouse> pageModel){
        PageModel<CkWarehouse> pageBean = manageService.queryCK(ckc,pageModel);
        return pageBean;
    }

    @RequestMapping("/ckUpdate")
    @ResponseBody
    public int queryCK(String id,String state,IDCondition idc){
        int count;
        if(state.equals("2")||state.equals("5")){
            count = manageService.updateCKState(id, state);
            List<Orderdetails> orderDetails = marketingApi.getOrderDetails(idc.getOid());
            System.out.println(orderDetails);
            for (Orderdetails orderDetail : orderDetails) {
                int pid = orderDetail.getProductId();
                int tid = orderDetail.getProduct().getTypeId();
                int fid = orderDetail.getProduct().getFirmId();
                int bid = orderDetail.getProduct().getType().getBrandId();
                int total = orderDetail.getPurchaseNum();
                idc.setPid(pid + "");
                idc.setTid(tid + "");
                idc.setFid(fid + "");
                idc.setBid(bid + "");
                idc.setTotal(total + "");
                if (state.equals("2")) {
                    int n = Integer.parseInt(idc.getTotal());
                    n = -n;
                    idc.setTotal(n + "");
                }
                System.out.println(idc);
                manageService.changRk2(idc);
            }
        }else {
            count = manageService.updateCKState(id,state);
        }
        return count;
    }

    @RequestMapping("/test")
    @ResponseBody
    public ResultMessage test(String orderId) {
        System.out.println(orderId);
        ResultMessage resultMessage = new ResultMessage();
        List<Orderdetails> orderDetails = marketingApi.getOrderDetails(orderId);
        resultMessage.setFlag(true);
        resultMessage.setMsg("查询成功");
        resultMessage.setResultData(orderDetails);
        resultMessage.setStatus("200");
        return resultMessage;
    }

    @RequestMapping("/addCK")
    @ResponseBody
    public int addCK(CkWarehouse ckw, String indent, Integer warehouseId){
        int count = manageService.addCK(ckw);
        marketingApi.updateOrderForWarehouse(indent,warehouseId);
        return count;
    }

    @RequestMapping("/cKDetail")
    @ResponseBody
    public ResultMessage cKDetail(){
        ResultMessage rm = marketingApi.queryOrderForWarehouse();
        return rm;
    }


}
